using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace Alex.Library.Cloud.VMWare.V51.Util
{
    public class ExtendedAppUtil : AppUtil
    {
        public ExtendedAppUtil(string name)
            : base(name)
        {
            Setup(name);
        }

        public SvcConnectionV25 SvcConnection25 { get; private set; }

        public ServiceUtilV25 Service25 { get; private set; }

        public static ExtendedAppUtil Initialize(string name, List<OptionSpec> userOptions,
            Dictionary<string, object> args)
        {
            ExtendedAppUtil cb = new ExtendedAppUtil(name);
            if (userOptions != null)
            {
                cb.AddOptions(userOptions);
            }
            cb.InitArgments(args);
            cb.Validate();
            return cb;
        }

        public static ExtendedAppUtil Initialize(string name, Dictionary<string, object> args)
        {
            return Initialize(name, null, args);
        }
        
        public void Setup(string name)
        {
            Service25 = new ServiceUtilV25();
            SvcConnection25 = new SvcConnectionV25("ServiceInstance");
        }

        public override void Connect()
        {
            
            try
            {
                LogMgr.Information("connect to service:{0}.", AppName);
                InitConnection();
                Service25.ClientConnect();
            }
            catch (Exception e)
            {
                LogMgr.Error("Exception running : " + AppName);
                LogMgr.Exception(e);
                throw e;
            }
        }

        public void Connect(Cookie cookie)
        {
            if (cookie == null)
            {
                Connect();
                return;
            }

            try
            {
                LogMgr.Information("use cookie[name:{0}, value:{1}, path:{2}] to connect to service:{3}.",
                    cookie.Name, cookie.Value, cookie.Path, AppName);
                InitConnection();
                Service25.ClientConnect(cookie);
            }
            catch (Exception e)
            {
                LogMgr.Error("Exception running : {0}", AppName);
                LogMgr.Exception(e);
                throw e;
            }
        }

        public override void InitConnection()
        {
            Service25.Init(this);
        }

        public override void DisConnect()
        {
            
            try
            {
                LogMgr.Information("disconnect to service:{0}.", AppName);
                Service25.ClientDisconnect();
            }
            catch (Exception e)
            {
                LogMgr.Error("Exception running : {0}", AppName);
                LogMgr.Exception(e);
                throw e;
            }
        }

        public override void Validate()
        {
            // DO NOTHING
        }
    }
}
